1514D - Cut and Stick - CodeForces Solution


binary search data structures greedy implementation sortings *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define ppb pop_back
#define int long long
#define itn long long
#define endl "\n"
#define fi first
#define se second
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i >=(b); i -= 1)
#define prdouble(x) cout<< fixed << setprecision(10)<< x;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int,int>> vpii;
typedef pair<ll, ll> pll;
typedef unordered_map<int,int> umap;
#define yeah cout<<"YES"<<endl;
#define nope cout<<"NO"<<endl;
#define all(v) v.begin(),v.end()
#define print(vec, l, r)                                                       \
    for (int i = l; i < r; i++)                                               \
        cout << vec[i] << " ";                                                 \
    cout << endl;
#define debug(v) cout<<v<<endl;

const int N=1e5+5;
const ll mod = 1e9 + 7;
const ll inf = 1e17;

long long ceil(int n,int i)
{
    return (n+i-1)/i;
}

long long power(long long a,long long b,long long m=mod){
    long long p=1;
    a%=m;
    while(b){
        if(b&1) p=(p*1LL*a)%m;
        a=(a*1LL*a)%m;
        b>>=1;
    }
    p%=m;
    return p;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


void solve()
{
    int n,q;
    cin>>n>>q;
    vector<int> v(n,0);
    vvi freq(n+1);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
        freq[v[i]].pb(i);
    }
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        l--,r--;
        int ans=1;
        forf(i,0,50)
        {
            int k=rng();
            k=k%(r-l+1);
            k+=l;
            int count=upper_bound(all(freq[v[k]]),r)-freq[v[k]].begin();
            count-=lower_bound(all(freq[v[k]]),l)-freq[v[k]].begin();
            ans=max(ans,2*count-(r-l+1));
        }
        debug(ans);
    }

    
    return;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
} 


Comments

Submit
0 Comments
More Questions

1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String